﻿using DbFactory;
using PlaneClean.Models;
using PlaneClean.Models.Group;
using PlaneClean.ViewModels;
using PlaneCleanEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// 角色管理
    /// </summary>
    public class GroupController : ManagerController
    {
        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<t_group> List(BaseRequest<ListRequest> request)
        {

            ListResponse<t_group> response = new ListResponse<t_group>();
            response.bodyList = new List<t_group>();

            //如果传入了userId  则查询 相关user的角色列表 否则查询所有
            List<t_group> groupList = new List<t_group>();
            if (request.body.userIdList != null)
            {
                List<int> groupIdList = dbFactory.Db.Queryable<t_user_group>().Where(a => request.body.userIdList.Contains(a.u_id)).Select(a => a.g_id).ToList();
                groupList = dbFactory.Db.Queryable<t_group>().Where(a => groupIdList.Contains(a.g_id)).ToList();
            }
            else
            {
                var sugar = dbFactory.Db.Queryable<t_group>();
                if (!string.IsNullOrEmpty(request.body.g_name))
                {
                    sugar.Where(a => a.g_name.Contains(request.body.g_name));
                }
                groupList = sugar.ToList();
            }
            response.bodyList.AddRange(groupList);

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Add(BaseRequest<t_group> request)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";
            //if (request.body == null)
            //{
            //    response.reason = "请求body不能为空";
            //    return response;
            //}
            if (request.body.g_id > 0)
            {
                response.reason = "有主键请调用更新接口";
                return response;
            }
            request.body.g_create_time = DateTime.Now;
            int pId = DbFactory.dbFactory.Db.Insertable(request.body).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
            if (pId <= 0)
            {
                response.reason = "插入数据库异常,新增失败";
                return response;
            }

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Update(BaseRequest<t_group> request)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";
            //if (request.body == null)
            //{
            //    response.reason = "请求body不能为空";
            //    return response;
            //}

            if (request.body.g_id <= 0)
            {
                response.reason = "没有主键请调用新增接口";
                return response;
            }
            t_group group = DbFactory.dbFactory.Db.Queryable<t_group>().First(a => a.g_id == request.body.g_id);
            if (group == null)
            {
                response.reason = "需要更新的飞机记录不存在";
                return response;
            }
            DbFactory.dbFactory.Db.Updateable(request.body).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Delete(BaseDeleteRequest request)
        {
            BaseResponse response = new BaseResponse();
            response.reason = request.idList.Count.ToString();

            if (request.idList == null || request.idList.Count <= 0)
            {
                response.reason = "请传入需要删除数据的主键";
                return response;
            }
            DbFactory.dbFactory.Db.Deleteable<t_group>().In(request.idList).ExecuteCommand();
            response.result = "OK";
            return response;
        }
    }
}